﻿using System;
using System.IO;

namespace SS.Ynote.Classic
{
    /// <summary>
    /// Finds a string in directory
    /// </summary>
    public class StringFinder
    {

        /// <summary>
        /// Default Constructor
        /// </summary>
        public StringFinder()
        {
        }
        /// <summary>
        /// Finds References in Directory
        /// </summary>
        /// <param name="output"></param>
        /// <param name="searchPath"></param>
        /// <param name="searchString"></param>
        /// <param name="searchpattern"></param>
        public void FindReferences(System.Collections.Generic.List<string> output, string searchPath, string searchString, string searchpattern, SearchOption option)
        {
            if (Directory.Exists(searchPath))
            {
                //searchpattern = "*.*";
                string[] files = Directory.GetFiles(searchPath, searchpattern, option);
 
                string line;

                // Loop through all the files in the specified directory & in all sub-directories
                foreach (string file in files)                {
                    using (StreamReader reader = new StreamReader(file))
                    {
                        int lineNumber = 1;
                        while ((line = reader.ReadLine()) != null)
                        {
                            if (line.Contains(searchString, StringComparison.OrdinalIgnoreCase))
                            {
                                output.Add(string.Format("{0}: Line : {1}", file, lineNumber));
                            }

                            lineNumber++;
                        }
                    }
                }
            }
        }
       /* static void Main()
        {
            string sourceFolder = @"C:\Test";
            string searchWord = ".class1";

            List<string> allFiles = new List<string>();
            AddFileNamesToList(sourceFolder, allFiles);
            foreach (string fileName in allFiles)
            {
                string contents = File.ReadAllLines(fileName);
                if (contents.Contains(searchWord))
                {
                    Console.WriteLine(fileName);
                }
            }

            Console.WriteLine(" ");
            System.Console.ReadKey();
        }*/

        public static void AddFileNamesToList(string sourceDir, System.Collections.Generic.List<string> allFiles)
        {

            string[] fileEntries = Directory.GetFiles(sourceDir);
            foreach (string fileName in fileEntries)
            {
                allFiles.Add(fileName);
            }

            //Recursion    
            string[] subdirectoryEntries = Directory.GetDirectories(sourceDir);
            foreach (string item in subdirectoryEntries)
            {
                // Avoid "reparse points"
                if ((File.GetAttributes(item) & FileAttributes.ReparsePoint) != FileAttributes.ReparsePoint)
                {
                    AddFileNamesToList(item, allFiles);
                }
            }

        }
    }
    /// <summary>
    /// Helper Methods
    /// </summary>
    static class HelperMethods
    {
        /// <summary>
        /// Determines whether the source string contains the specified value.
        /// </summary>
        /// <param name="source">The String to search.</param>
        /// <param name="toCheck">The search criteria.</param>
        /// <param name="comparisonOptions">The string comparison options to use.</param>
        /// <returns>
        ///   <c>true</c> if the source contains the specified value; otherwise, <c>false</c>.
        /// </returns>
        public static bool Contains(this string source, string value, StringComparison comparisonOptions)
        {
            return source.IndexOf(value, comparisonOptions) >= 0;
        }
    }
}
